<?php

namespace catchAdmin\model;

class News extends ModelApp
{

    protected $append = [
        'created_at_show',
        'cover_show',
        'short_content',
        'banner_is_show',
    ];


    public function newsClass()
    {
        return $this->hasOne(NewsClass::class, 'id', 'news_class_id')->bind([
            'class_name' => 'class_name',
        ]);
    }


    /**
     * banner_is_show
     */
    public function getBannerIsShowAttr()
    {
        $list = [
            '1' => '否',
            '2' => '是',
        ];

        return $list[$this->getOrigin('banner_is')] ?? '';
    }


    /**
     * created_at_show
     */
    public function getCreatedAtShowAttr()
    {
        return date('Y年m月d日 H:i', $this->getOrigin('created_at'));
    }


    /**
     * cover_show
     */
    public function getCoverShowAttr()
    {
        if (empty($this->getOrigin('cover'))) {
            return '';
        }

        if (substr($this->getOrigin('cover'), 0, 4) == 'http') {
            return $this->getOrigin('cover');
        }

        return request()->domain() . $this->getOrigin('cover');
    }


    /**
     * short_content
     */
    public function getShortContentAttr()
    {
        return mb_substr(strip_tags($this->content), 0, 20);
    }


    public function searchContentAttr($query, $value, $data)
    {
        $query->where('content', 'like', '%' . $value . '%');
    }


    public function searchFaNameAttr($query, $value, $data)
    {
        $query->where('fa_name', 'like', '%' . $value . '%');
    }


    public function searchTitleAttr($query, $value, $data)
    {
        $query->where('title', 'like', '%' . $value . '%');
    }


    public function searchNewsClassIdAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('news_class_id', $value);
        }
    }


}
